<template>
    <el-dialog :title="form.noticeId ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
        <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <el-form-item label="栏目">
              <el-select v-model="param.columnId" placeholder="请选择栏目">
                <el-option v-for="item in columnList" :key="item.columnId" :label="item.columnName" :value="item.columnId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <el-form-item label="发布时间" >
              <el-date-picker
                  v-model="param.createTime"
                  type="daterange"
                  start-placeholder="Start date"
                  end-placeholder="End date"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DDTHH:mm:ss"
                  :disabled="!param.columnId"
                  @change="getWzArticleList(param)"
              />
            </el-form-item>
          </el-col>

        </el-row>
       <el-row :gutter="24">
    <el-col :span="24" class="mb20">
      <el-form-item label="文章" prop="articleId">
          <el-select v-model="form.articleId" placeholder="请选择文章" @change="console.log(typeof form.articleId)">
            <el-option v-for="item in articleList" :key="item.articleId"
                       :label="item.title"
                       :value="item.articleId" ></el-option>
          </el-select>
        </el-form-item>
      </el-col>

			</el-row>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="WzNoticeAnnouncementDialog">
import { useMessage } from "/@/hooks/message";
import { getNa, addObj, putNa } from '/@/api/bzg/wzNoticeAnnouncement'
import {getSonList} from "/@/api/wzjiang/wzColumn";
import {getWzArticleNAByColumnId, putObj} from "/@/api/Article/wzArticle";
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
		noticeId:'',
	  articleId: '',

});
const param = reactive({
    columnId: '',
  createTime: '',
});
// 定义校验规则
const dataRules = ref({
        articleId: [{required: true, message: '文章不能为空', trigger: 'blur'}]
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.noticeId = ''
  param.createTime = ''
  param.columnId = ''
  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取wzNoticeAnnouncement信息
  if (id) {
    form.noticeId = id
    getwzNoticeAnnouncementData(id)
  }
};

const columnList = ref();
const getColumnList = async () => {
  getSonList().then((res: any) => {
    columnList.value = res.data
  })
}
getColumnList();

const articleList = ref([]);
const getWzArticleList = async (params: any) => {
  await getWzArticleNAByColumnId(params).then((res: any) => {
    articleList.value = res
  })
}
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;
	try {
    loading.value = true;
		form.noticeId ? await putNa(form) : await addObj(form);
		useMessage().success(form.noticeId ? '修改成功' : '添加成功');
    //将文章的通知公告状态置为1
    if(form.articleId){
      const articleParams = {
        articleId: form.articleId,
        noticeAnnouncement: "1"
      }
      putObj(articleParams)
    }
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getwzNoticeAnnouncementData = (id: string) => {
  // 获取数据
  loading.value = true
  getNa(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};


// 暴露变量
defineExpose({
  openDialog
});
</script>